package server;

import java.math.*;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.*;
import java.rmi.server.*;
import java.util.Properties;

//
// PowerServiceServer
//
// Server for a RMI service that calculates powers
//
public class PowerServiceServer extends UnicastRemoteObject implements
		PowerService {
	public PowerServiceServer() throws RemoteException {
		super();
	}

	// Calculate the square of a number
	public BigInteger square(int number) throws RemoteException {
		String numrep = String.valueOf(number);
		BigInteger bi = new BigInteger(numrep);

		// Square the number
		bi.multiply(bi);

		return (bi);
	}

	// Calculate the power of a number
	public BigInteger power(int num1, int num2) throws RemoteException {
		String numrep = String.valueOf(num1);
		BigInteger bi = new BigInteger(numrep);

		bi = bi.pow(num2);
		return bi;
	}

	public static void main(String args[]) {
		try {
			System.out.println(java.net.InetAddress.getLocalHost());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Assign a security manager, in the event that dynamic
		// classes are loaded

		if (System.getSecurityManager() == null)
			System.setSecurityManager(new RMISecurityManager());
		// System.out.println(System.getSecurityManager());
		// Create an instance of our power service server ...
		PowerServiceServer svr = null;
		try {
			svr = new PowerServiceServer();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("before");
		// ... and bind it with the RMI Registry

			try {
				Naming.bind("PowerService", svr);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				System.out.println("remote exception");
				e.printStackTrace();
			} catch (AlreadyBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		System.out.println("Service bound....");
	}
}